<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">

      <el-form-item label="发票编号" prop="invoiceCode">
        <el-input v-model="queryParams.invoiceCode" placeholder="请输入发票编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="审核状态" prop="payStatus">
        <el-select clearable placeholder="请选择审核状态"  v-model="queryParams.assessStatus">
          <el-option :key="dict.value"
                     :label="dict.label" :value="dict.value" v-for="dict in dict.type.apply_assess_status"/>
        </el-select>
      </el-form-item>

      <el-form-item label="回款状态" prop="payStatus">
        <el-select clearable placeholder="请选择回款状态"  v-model="queryParams.payStatus">
          <el-option :key="dict.value"
                     :label="dict.label" :value="dict.value" v-for="dict in dict.type.apply_pay_status"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
      <el-table-column label="客户名称 " align="center" prop="lvBuy.customerName" width="250" show-overflow-tooltip/>
      <el-table-column label="供应商名称 " align="center" prop="goodsSupplier.name" width="230"/>
      <el-table-column align="center" label="付款类别" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_apply_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="发票编号" align="center" prop="invoiceCode"/>
      <el-table-column label="申请付款金额" align="center" prop="totalMoney"/>
      <el-table-column label="已付金额" align="center" prop="paidMoney"/>
      <el-table-column label="剩余金额" align="center" prop="remainMoney"/>
      <el-table-column align="center" label="审核状态" prop="assessStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_assess_status" :value="scope.row.assessStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="付款状态" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)" v-hasPermi="['develop:PayApply:edit']">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <pay-apply-detail ref="payApplyDetail"></pay-apply-detail>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import PayApplyDetail from "@/views/develop/payApply/module/PayApplyDetail.vue";

export default {
  name: "FapiaoList",
  dicts: ["pay_apply_type", "apply_pay_status", "apply_assess_status"],
  components: {
    PayApplyDetail,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款申请单表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        buyId: null,
        invoiceCode: null,
        assessStatus: null,
        orderByColumn: "applyTime",
        isAsc: "desc"
      },
      url: {
        list: '/payApply/payApply/list',
        remove: '/payApply/payApply/',
        export: '/payApply/payApply/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询付款申请单列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAddMaterial() {
      this.$refs.buyListModule.show();
    },
    handleAddHardwareMaterial() {
      this.$refs.buyListModuleHardware.show();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `payApply_${new Date().getTime()}.xlsx`)
    },


    handleAddHardware() {
      this.$refs.hardwareListForm.show();
    },


    payRecordForm(row) {
      this.$refs.payRecordForm.handleAddByApply(row);
    },

    handleDetail(row) {
      this.$refs.payApplyDetail.showDetail(row.id, row.buyId);
    },
    payRecordList(row) {
      let applyId = row.id;
      this.$refs.payRecordList.showPayRecordList(applyId);
    },


    handleAssess(row) {
      this.$refs.payAssessForm.handleUpdate(row);
    }


  }
};
</script>
